﻿@* Generator: Template
    GeneratePrettyNames : true *@
@functions{
    public Compile.Template.Startup.NFinal.ActionSearchModel Model { get; set; }
}
namespace NFinal
{
    public class ActionSearch
    {
        public static int Search(string requestedPath)
        {
			//重写虚拟目录
			@foreach(var rewriteDirectory in Model.rewriteData.rewriteDirectoryList){
			@:if(requestedPath.StartsWith("@rewriteDirectory.from"))
			@:{
			@:	requestedPath = "@rewriteDirectory.to" + requestedPath.Remove(0,"@rewriteDirectory.from".Length);
			@:}
			}
			//重写虚拟路径,也可以把此部分放到find中
			switch (requestedPath)
			{
				//网站图标
				case "/favicon.ico": return 0;
				@foreach(var rewriteFile in Model.rewriteData.rewriteFileList){
				@:case "@rewriteFile.from":requestedPath = "@rewriteFile.to"; break;
				}
				default: break;
			}
            //获取actionUrl
            string actionUrl = GetActionUrl(requestedPath);
            int find = GetActionId(actionUrl, requestedPath);
            return find;
        }
        /// <summary>
        /// 计算actionUrl
        /// </summary>
        /// <param name="requestedPath"></param>
        /// <returns></returns>
        private static string GetActionUrl(string requestedPath)
        {
            //找到最后一个.的位置
            int position = requestedPath.LastIndexOf('.');
            bool hasExtension = false;
            
            //判断是否有后缀（后缀首字母不能为数字，且长度必须大于0小于5）
            int extesionLength = requestedPath.Length - position-1;
            if (extesionLength > 0 && extesionLength < 5)
            {
                char ch = requestedPath[position + 1];
				//.后面不是数字
                if (ch > '9' || ch < '0')
                {
					hasExtension = true;
                }
            }
            
            //指向ActionUrl结尾的位置
            int pointer = 0;
            string actionUrl = requestedPath;
            bool hasGet = false;
            //如果有后缀
            if (hasExtension)
            {
				if(position>2)
				{
					hasGet = int.TryParse(requestedPath.Substring(position - 2, 2), out pointer);
				}
            }
            //如果没有后缀
            else
            {
				if(requestedPath.Length>2)
				{
					hasGet = int.TryParse(requestedPath.Substring(requestedPath.Length - 2, 2), out pointer);
				}
            }
            //取出ActionUrl
            if (hasGet)
            {
                if (pointer > -1 && pointer < requestedPath.Length)
                {
                    actionUrl = requestedPath.Substring(0, pointer + 1);
                }
            }
            return actionUrl;
        }
        /// <summary>
        /// 查询actionUrl
        /// </summary>
        /// <param name="app"></param>
        /// <param name="actionUrl"></param>
        /// <param name="requestedPath"></param>
        /// <returns></returns>
        private static int GetActionId(string actionUrl, string requestedPath)
        {
            int find = 0;
            //无参数
            switch (requestedPath)
            {
                @{int i=1;
				foreach(var fileData in Model.controllerFileDataList){
				foreach(var classData in fileData.ClassDataList){
				foreach(var methodData in classData.MethodDataList){
                if(methodData.isPublic){
					if(methodData.hasParameters==false){
				@:case "@methodData.urlParser.actionUrl":
                @:    find = @i; break;
					}
				i++;
				}}}}}
                default: find = 0; break;
            }
			//有参数
			if(find==0)
			{
				switch (actionUrl)
				{
					@{i=1;
					foreach(var fileData in Model.controllerFileDataList){
					foreach(var classData in fileData.ClassDataList){
					foreach(var methodData in classData.MethodDataList){
					if(methodData.isPublic){
						if(methodData.hasParameters==true){
					@:case "@methodData.urlParser.actionUrl":
					@:	find = @i; break;
						}
					i++;
					}}}}}
					default: find = 0; break;
				}
			}
            return find;
        }
    }
}